package com.hethongviet.doorcontrollercall.page;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.hethongviet.preventingmachine.AppMain;
import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.Utils;
import com.hethongviet.preventingmachine.helps.ClientSQLite;
import com.hethongviet.preventingmachine.models.Client;

public class PageSettingsActivity extends Activity
{

	private List<Client> _ClientList = null;

	private LinearLayout mListDeviceControl = null;

	private ClientSQLite _ClientSQLite = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		Utils.prepareWindow(this);

		setContentView(R.layout.page_settings);

		mListDeviceControl = (LinearLayout) findViewById(R.id.layout_list_device_name);

		initDB();

		try
		{
			_ClientList = _ClientSQLite.getAll();
		} catch (Exception e)
		{
			e.printStackTrace();
		}

		fillListDeviceControl();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.settings, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if (item.getItemId() == R.id.menu_add)
		{
			Intent intent = new Intent(PageSettingsActivity.this,
					PageChangeContactActivity.class);
			startActivityForResult(intent, PageChangeContactActivity.REQUEST_ADD);
		}
		return true;
	}

	private void fillListDeviceControl()
	{
		mListDeviceControl.removeAllViews();

		if (_ClientList == null || _ClientList.size() == 0)
		{
			return;
		}
		
		final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		for (int indexClient = 0; indexClient < _ClientList.size(); indexClient++)
		{
			final Client client = _ClientList.get(indexClient);

			final View view = layoutInflater.inflate(
					R.layout.layout_one_contact, null);

			Button buttonName = (Button) view.findViewById(R.id.button_one_layout_device_control);
			Button buttonEdit = (Button) view.findViewById(R.id.button_one_layout_edit);
			Button buttonDelete = (Button) view.findViewById(R.id.button_one_layout_delete);

			buttonName.setText(client.getDeviceName());
			buttonName.setTextSize(20);
			
			buttonName.setEnabled(false);
			buttonDelete.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					mListDeviceControl.removeView(view);
					_ClientSQLite.delete(client);
				}
			});

			buttonEdit.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent = new Intent(PageSettingsActivity.this,
							PageChangeContactActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable(PageChangeContactActivity.EXTRA_CLIENT, client);
					intent.putExtras(bundle);
					startActivityForResult(intent, PageChangeContactActivity.REQUEST_EDIT);
				}
			});

			mListDeviceControl.addView(view);
		}
	}

	private void initDB()
	{
		_ClientSQLite = new ClientSQLite(this,
				AppMain.getInstance().getDBName());
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode == RESULT_OK)
		{
			if (data != null)
			{
				Client client = (Client) data.getSerializableExtra(PageChangeContactActivity.EXTRA_CLIENT);
//				
//				Bundle bundle
//				 = data.getExtras();
//				
//				client = (Client) bundle.getSerializable(PageChangeContactActivity.EXTRA_CLIENT);
//				
				_ClientSQLite.insert(client);
				_ClientList = _ClientSQLite.getAll();
				fillListDeviceControl();
			}
		}
	}

}
